<?php

/* @var $this yii\web\View */

use yii\helpers\Url;
use common\helpers\Render;
use common\models\ExternalLink;

$this->addCrumbs('外链列表', 'external-link/external-link-list');
$this->title = isset($data['id']) ? '修改' : '添加'. '外链';
?>

<form class="box-content flyer-form pane gap" id="flyer-create" action="<?= Url::to('@web/external-link/external-link-'.(isset($data['id']) ? 'update?id='.$data['id'] : 'insert')) ?>" method="post">
    <div class="form-item checker">
        <div class="input-title">网站名称</div>
        <div class="input-block"><input class="flyer-input" type="text" name="name" value="<?= Render::value($data, 'name') ?>" placeholder="name."></div>
    </div>
    <div class="form-item checker">
        <div class="input-title">网址</div>
        <div class="input-block"><input class="flyer-input" type="text" name="link" value="<?= Render::value($data, 'link') ?>" placeholder="link."></div>
    </div>
    <div class="form-item checker">
        <div id="uploader"></div>
        <input id="image-input" name="image" type="text" value="<?= Render::value($data, 'image') ?>" style="display:none;">
    </div>
    <div class="form-item">
        <div class="input-block tr bdn"><button class="flyer-button normal border-round" id="save-button" type="submit"><i class="icon-save"></i> 保 存</button></div>
        <textarea id="flyer-create-json" data-form="#flyer-create" style="display:none;"><?= ExternalLink::checker(isset($data['id']) ? 'update' : 'insert') ?></textarea>
    </div>
    <input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken() ?>">
</form>

<script src="<?= Render::static('flyer/flyer.class.js') ?>"></script>
<script src="<?= Render::static('flyer/checker.class.js') ?>"></script>
<!-- 图片批量上传功能插件引入 -->
<link href="<?= Render::static('uploader/loader.css') ?>" rel="stylesheet">
<script src="<?= Render::static('uploader/loader.core.single.class.js') ?>"></script>
<script src="<?= Render::static('uploader/loader.image.class.js') ?>"></script>
<script>
    $(document).ready(function() {
        // Form 元素初始化
        (new flyer).init({ form: '#flyer-create' });
        // 表单数据验证
        (new checker).init({ ruleDom: '#flyer-create-json' });
        // 初始化 图片上传插件
        (new loaderImage()).init({
            conter: '#uploader',
            prevLoaders: "<?= isset($data['id']) ? Render::upload(Render::value($data, 'image')) : '' ?>",
            action: '<?= Url::to('@web/external-link/external-link-uploader') ?>',
            onSuccess: function (file, response) {
                response = $.parseJSON(response);
                $('#image-input').val(response.path);
            },
            onFailure: function (file) {
                layer.alert('图片 ' + file.name + ' 上传失败！', {icon: 2});
            }
        });
    });
</script>